<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/6
 * Time: 16:16
 */

namespace app\api\controller\v4;

use app\common\model\AppVersion;

/**
 * @title App版本更新接口
 * @desc 【附带 控制星盘数据来源为 客户端本地获取或服务器获取 支付方式控制】
 * @url /api/v4/astro_update
 * @version 1.4.0
 * Class AppVersionUpdate
 * @package app\api\controller\v4
 */
class AppVersionUpdate
{
    /**
     * 获取版本更新信息
     * @return \think\response\Json
     * @throws \Exception
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $headers = request()->header();

        $headers['app-type'] = strtolower($headers['app-type']);
        $data = (new \app\common\cachemodel\AppVersion())->getVersionData(['type' => $headers['app-type'], 'version' => ($headers['version'] ?? '')]);
        if ($data)
        {
            $default_pay_type = 2;
            if ($headers['app-type'] == 'ios')
            {
                $apple_pay = explode(',', $data['goods_pay_type']);
                if (in_array($default_pay_type, $apple_pay))
                {
                    array_unshift($apple_pay, $default_pay_type);
                }
                $data['pay_type_data'] = implode(',', array_unique($apple_pay));
            } else
            {
                $data['pay_type_data'] = '1,2';// 5 京东支付 已下架
            }
//        $data['jd_pay_des'] = config('jdpay.des');
            $data['jd_pay_des'] = '';
            unset($data['is_apple_pay'], $data['goods_pay_type'], $data['coin_pay_type']);
            /**
             * 仅针对安卓1.9.0版本开启热更新
             */
            if (isset($headers['app-type']) && $headers['app-type'] == 'android' && isset($headers['version']) && $headers['version'] === '1.9.0')
            {
//            $data['is_hot_update'] = config('app.is_hot_update');
                $data['is_hot_update'] = 1;
            }
            return show(config('code.success'), '加载成功', $data);
        }

        return show(config('code.error'), 'app更新信息获取失败');
    }

}
